﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace MDK.Common
{
    public class HandoverData
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        static string _staticurl = "http://madik.gosafenet.com.cn/Data/UploadData";
        static string _SystemID = "35f05d7e31d64384b8ee16b31da2a952";
        static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
        
        /// <summary>
        /// 发送机械臂数据
        /// </summary>
        /// <param name="DeviceIP">设备IP</param>
        /// <param name="ClassName">类名</param>
        /// <param name="Device">设备</param>
        /// <param name="Coord">当前坐标</param>
        /// <param name="Speed">速度</param>
        /// <param name="IsWrong">是否错误0正常1错误</param>
        /// <param name="ErrorCode">错误代码</param>
        public static void SendArmData(EnumCmdCode CmdCode,string DeviceIP,string ClassName,string Device,int Coord,int Speed,int IsWrong=0,int ErrorCode=0)
        {
            try
            {
                var parameter = new
                {
                    success = true,
                    CmdCode = CmdCode,
                    Value = $"{_SystemID},{GetDeviceID(DeviceIP)},{ClassName},{Device},{Coord.ToString("D8")}{Speed.ToString("D8")}{IsWrong.ToString("D8")}{ErrorCode.ToString("D8")}",
                    Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

                };
                _logger.Info(parameter.Value);
                HttpPost(_staticurl, JsonConvert.SerializeObject(parameter));
            }
            catch
            {

            }
           
        }
        /// <summary>
        /// 发送其它动作状态改变数据
        /// </summary>
        /// <param name="DeviceIP">设备IP</param>
        /// <param name="ClassName">类名</param>
        /// <param name="Device">设备</param>
        /// <param name="State">运动状态</param>
        public static void SandStateChangeData(EnumCmdCode CmdCode,string DeviceIP, string ClassName, string Device,int State)
        {
            try
            {
                var parameter = new
                {
                    success = true,
                    CmdCode = CmdCode,
                    Value = $"{_SystemID},{GetDeviceID(DeviceIP)},{ClassName},{Device},{State.ToString("D8")}",
                    Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

                };
                _logger.Info(parameter.Value);
                HttpPost(_staticurl, JsonConvert.SerializeObject(parameter));
            }
            catch
            {

            }
        }
        /// <summary>
        /// 发送冷藏温度
        /// </summary>
        /// <param name="DeviceIP">设备IP</param>
        /// <param name="ClassName">类名</param>
        /// <param name="Device">设备</param>
        /// <param name="temperature">当前温度</param>
        /// <param name="Maxtemperature">上限温度</param>
        /// <param name="Mintemperature">下限温度</param>
        public static void SandKeepTemperatureData(EnumCmdCode CmdCode,string DeviceIP, string ClassName, string Device, int temperature, int Maxtemperature, int Mintemperature)
        {
            try
            {
                var parameter = new
                {
                    success = true,
                    CmdCode = CmdCode,
                    Value = $"{ _SystemID},{GetDeviceID(DeviceIP)},{ ClassName},{ Device},{ temperature.ToString("D8")}{ Maxtemperature.ToString("D8")}{ Mintemperature.ToString("D8")} ",
                    Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                _logger.Info(parameter.Value);
                HttpPost(_staticurl, JsonConvert.SerializeObject(parameter));
            }
            catch
            {

            }
        }
        static string GetDeviceID(string plcIP)
        {
            switch (plcIP.Substring(plcIP.Length - 3, 3)) {
                case "101":
                    return "6f4d5712-e8f3-4f8a-af1d-32cb7831c0ee";
                case "102":
                    return "e53d99d1-8eb6-443d-938e-70eed280dc5e";
                case "103":
                    return "7e06fb37-efcf-4762-b4fe-b552d3a0f876";
                default:
                    return "d5017ce2-28f7-4292-bb3c-207be2e89fa4";
            }

        }
        /// <summary>
        /// 发送离心机当前状态数据
        /// </summary>
        /// <param name="DeviceIP">设备IP</param>
        /// <param name="ClassName">类名</param>
        /// <param name="Device">设备</param>
        /// <param name="State">运行状态0位运行1运行</param>
        /// <param name="temperature">当前温度</param>
        /// <param name="Speed">当前转速</param>
        /// <param name="Balance">当前不平衡值</param>
        /// <param name="Time">运行时间</param>
        /// <param name="IsWrong">是否错误0正常1错误</param>
        /// <param name="ErrorCode">错误代码</param>
        public static void SandCentrifugeData(EnumCmdCode CmdCode,string DeviceIP, string ClassName, string Device,int State, int temperature, int Speed, int Balance,int Time, int IsWrong = 0, int ErrorCode = 0)
        {
            try
            {
                var parameter = new
                {
                    success = true,
                    CmdCode = CmdCode,
                    Value = $"{_SystemID},{GetDeviceID(DeviceIP)},{ClassName},{Device},{State.ToString("D8")}{temperature.ToString("D8")}{Speed.ToString("D8")}{Time.ToString("D8")}{IsWrong.ToString("D8")}{ErrorCode.ToString("D8")}",
                    Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

                };
                _logger.Info(parameter.Value);
                HttpPost(_staticurl, JsonConvert.SerializeObject(parameter));
            }
            catch
            {

            }
        }
        /// <summary>
        /// Post提交数据请求
        /// </summary>
        /// <param name="POSTURL">请求提交的地址</param>
        /// <param name="PostData">提交的数据(字符串)</param>
        /// <returns></returns>
        static string HttpPost(string POSTURL, string PostData)
        {
            //发送请求的数据
            WebRequest myHttpWebRequest = WebRequest.Create(POSTURL);
            myHttpWebRequest.Method = "POST";
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byte1 = encoding.GetBytes(PostData);
            myHttpWebRequest.ContentType = "application/json";

            myHttpWebRequest.ContentLength = byte1.Length;
            Stream newStream = myHttpWebRequest.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            newStream.Close();

            HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();

            //发送成功后接收返回的XML信息
            response = (HttpWebResponse)myHttpWebRequest.GetResponse();

            string lcHtml = string.Empty;
            Encoding enc = Encoding.GetEncoding("UTF-8");
            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream, enc);
            lcHtml = streamReader.ReadToEnd();
            return lcHtml;
        }
    }
    public enum EnumCmdCode
    {
        /// <summary>
        /// 机械臂运动
        /// </summary>
        ArmMove = 863,
        /// <summary>
        /// 皮带（挡板，推杆）运动
        /// </summary>
        StorageMove = 864,
        /// <summary>
        /// 冷藏温度
        /// </summary>
        StorageTemperature=865,
        /// <summary>
        /// 离心机
        /// </summary>
        Centrifuge=866,
        /// <summary>
        /// 传感器状态
        /// </summary>
        SensorState=867,
        /// <summary>
        /// 其它动作运行
        /// </summary>
        OtherAction=868
    }
}
